<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <!--{include file='comm/header.tpl'}-->
    <title>添加发布SQL</title>
</head>
<body>
<div style="width:100%">

    <div class="items clearfix">
        <h2 style="" class="tal">正在操作：添加发布SQL</h2>
        <div class="items2">

            <form id="sql_form" onsubmit="return false;">
                <table>
                    <tbody>
                        <tr>
                            <td class="tar" style="width:10%;">项目：</td>
                            <td>
                                <select id="projectID" name="projectID" style="width:160px;">
                                    <option value="">请选择</option>
                                    <!--{foreach from=$projectList item=project}-->
                                    <option value="<!--{$project.projectID}-->" ><!--{$project.projectName}--></option>
                                    <!--{/foreach}-->
                                </select>
                                <font color="red">*</font>
                            </td>
                        </tr>
                        <tr>
                            <td class="tar">数据库：</td>
                            <td id="database">

                            </td>
                        </tr>
                        <tr>
                            <td class="tar" valign="top">SQL：</td>
                            <td>
                                <textarea id="sqltxt" name="sqltxt" style="width:98%;height:300px;"></textarea>
                                <font color="red">*</font>
                                <p style="color: red;font-weight: bold;">注：多条SQL，以"；"隔开<p/>
                            </td>
                        </tr>
                        <tr>
                            <td class="tar" valign="top" style="padding-top: 20px;">备注：</td>
                            <td style="padding-top: 20px;">
                                <textarea id="memo" name="memo" style="width:98%;height:100px;"></textarea>
                            </td>
                        </tr>

                        <input type="hidden" name="creator" value="<!--{$currentUser}-->">

                    </tbody>
                </table>
            </form>

        </div>
    </div>

    <div style="height: 40px;"></div>

    <div class="bottom_fixbox">
        <div class="rectbox tac">
            <input type="button" class="btn" onclick="sqlPub.check();" value="发布" />&nbsp;&nbsp;&nbsp;
            <input type="button" class="btn" onclick="E.closeWindows();" value="关闭" />
        </div>
    </div>



</div>

<script type="text/javascript">

    //全局变量
    var currentUser = '<!--{$currentUser}-->';

    //变更项目，显示其数据库
    $("#projectID").change(function(){

        var projectID = $(this).val();

        if( projectID == '' ){
            $("#database").html('');
            return false;
        }

        jquery_ajax(3,{projectID:projectID},sqlPub.showDB);

    })

    var sqlPub = {

        dt: "",

        check: function() {

            this.dt = E.getFormValues('sql_form');

            var error_msg = "";

            //记录选中的数据库
            var checkedArr = new Array();

            if( this.dt.projectID == '' ){

                error_msg += "请选择发布的项目<br />";

            }else{

                $("input[name*='chk_']").each(function(k,v){

                    var value = v.value;

                    if( v.checked == true ){
                        checkedArr.push(value);
                    }

                })

                if( checkedArr.length == 0 )
                    error_msg += "请选择发布的数据库<br />";

            }

            if (this.dt.sqltxt == "")
                error_msg += "发布SQL不能为空<br />";

            if (error_msg != "")
                E.alert(error_msg);
            else {

                this.dt['checkedArr'] = checkedArr;
                this.add();

            }

        },

        add: function() {
            $.ajax({
                type: "POST",
                url: "/invokingAjax-backend/project/projectSql.ajax?operFlg=2",
                dataType: "JSON",
                data: this.dt,
                success: function( result_obj ) {

                    if (result_obj.code == 200) {

                        E.opener("sqlProject.load");

                        $("#sqltxt").val('');
                        $("#memo").val('');

                        E.alert( result_obj.message , 2);

                    } else {

                        E.alert(result_obj.message);

                    }

                }
            });
        },

        //显示数据库
        showDB:function( obj ){

            if( obj.code == 200 ){

                $("#database").html('');

                var params = obj.data;

                var html = '';

                for(var i=0; i<params.length; i++){

                    html += '<input type="checkbox" name="chk_'+params[i].dbID+'" id="chk_'+params[i].dbID+'" value="'+params[i].dbID+'">&nbsp;' + params[i].db_byname + '&nbsp;&nbsp;&nbsp;';

                }

                $("#database").html( html );

            }else{
                E.alert('系统错误，请与管理员联系');
            }

        }

    };

    function jquery_ajax(flg,data,func){
        $.ajax({
            type: "post",
            url: "/invokingAjax-backend/project/projectSql.ajax?operFlg="+flg,
            dataType: "json",
            data: data,
            success: function( result_obj ) {
                func(result_obj);
            },
            error : function(msg){
                alert('网络不给力，请重新尝试...');
            }
        });
    }


</script>

</body>

</html>
